package com.aurora.leetcode.answer;

import com.alibaba.fastjson2.JSON;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author : Aurora
 * @date : 2024/5/23 09:41
 * @description <p>18. 四数之和</p>
 * <p>给你一个由 n 个整数组成的数组 nums ，和一个目标值 target 。</p>
 * <p>请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]]
 * （若两个四元组元素一一对应，则认为两个四元组重复）：</p>
 * <p>0 <= a, b, c, d < n</p>
 * <p>a、b、c 和 d 互不相同</p>
 * <p>nums[a] + nums[b] + nums[c] + nums[d] == target</p>
 * <p>你可以按 任意顺序 返回答案 。</p>
 * <ul>>>>>>example1<<<<<</ul>
 * <p>输入：nums = [1,0,-1,0,-2,2], target = 0</p>
 * <p>输出：[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]</p>
 * <ul>>>>>>example2<<<<<</ul>
 * <p>输入：nums = [2,2,2,2,2], target = 8</p>
 * <p>输出：[[2,2,2,2]]</p>
 * <ul>>>>>>example3<<<<<</ul>
 * <p>输入：nums = [1000000000,1000000000,1000000000,1000000000,1000000000], target = -294967296</p>
 * <p>输出：[]</p>
 */
public class FourSum_18 {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; ++i) {
            //剪枝
            if(nums[i] > 0 && nums[i] > target) return  result;//参照官方改进
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < nums.length; ++j) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                int left = j + 1;
                int right = nums.length - 1;
                boolean overflow = false;
                while (left < right) {
//                    int num1 = nums[i] + nums[j];
//                    int num2 = nums[left] + nums[right];
//                    boolean a = judgeOverflow(nums[i], nums[j]);
//                    boolean b = judgeOverflow(nums[left], nums[right]);
//                    boolean c = judgeOverflow(num1, num2);
//                    if(a || b || c) overflow = true;
//                    int temp = num1 + num2;
                    long temp = (long)nums[i] + nums[j] + nums[left] + nums[right];
                    if (temp < target) {
                        left++;
                    } else if (temp > target) {
                        right--;
                    } else {
                        if(!overflow){
//                            List<Integer> list = new ArrayList<>();
//                            list.add(nums[i]);
//                            list.add(nums[j]);
//                            list.add(nums[left]);
//                            list.add(nums[right]);
//                            result.add(list);
                            result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        }
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return result;
    }
    public boolean judgeOverflow(int num1, int num2){
        if(num1>0 && num2>0 && num1>Integer.MAX_VALUE-num2){
            return true;
        }
        if(num1<0 && num2<0 && num1<Integer.MIN_VALUE-num2){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        FourSum_18 obj = new FourSum_18();
////        int[] arr = new int[]{1,0,-1,0,-2,2};
////        System.out.println(JSON.toJSONString(obj.fourSum(arr, 0)));
//        int[] arr = new int[]{2,2,2,2,2};
//        System.out.println(JSON.toJSONString(obj.fourSum(arr, 8)));
//        int[] arr = new int[]{1000000000,1000000000,1000000000,1000000000,1000000000};
//        System.out.println(JSON.toJSONString(obj.fourSum(arr, -294967296)));

        int[] arr = new int[]{0,0,0,1000000000,1000000000,1000000000,1000000000};
        System.out.println(JSON.toJSONString(obj.fourSum(arr, 1000000000)));


    }
}
